<?php 
ob_start();
session_start();
include("utils.php");

?>
<script type="text/javascript">
        var GB_ROOT_DIR = "./greybox/";
</script>
<script type="text/javascript" src="greybox/AJS.js"></script>
<script type="text/javascript" src="greybox/AJS_fx.js"></script>
<script type="text/javascript" src="greybox/gb_scripts.js"></script>
<link rel="stylesheet" href="style/style.css" type="text/css" />
<link rel="stylesheet" href="css/global.css" type="text/css" />

<?php

//main_content("mainBuySpace();");
if(!isset($_SESSION[siteuserId]) || $_SESSION[siteuserId]=='')
	echo '<script>window.location="index.php"</script>';
// Start Coming from popup
if(isset($_GET['form_id']) && $_GET['form_id'] != '')
{
	$_SESSION['form_id'] = $_GET['form_id'];
}
// end coming from pop
if($_SESSION['langId']!="")
{
		$langId=$_SESSION['langId'];
}
else
{
		$langId="1";
}


if(isset($_POST['hidden_field']) && $_POST['hidden_field'] == 'www')
{
	$garbage = 'garbage';
	$pop_query = "insert into temp_adgroup_ad(form_id,heading,ad_description,dest_url,personal_reference,garbage_val,siteuserId) values('".$_SESSION['form_id']."','".$_POST['txtHeadline']."','".$_POST['text_description']."','".$_POST['txtUrl']."','".$_POST['txtAdName']."','".$garbage."','".$_SESSION[siteuserId]."') " ;
	
	$res = mysql_query($pop_query);
	$inner_res = mysql_query("select * from temp_adgroup_ad where form_id = '".$_SESSION['form_id']."'");
	$startTable = '<table width="100%" style="color:#000000" cellpaddin="0" cellspacing="0" bgColor="#666666">';
	$endTable = '</table>';
	$i = 1;
	while($inner_row = mysql_fetch_array($inner_res))
	{
		$rowStr .= '<tr style="cursor:pointer;" onclick="callOpenExistingAdgroup('.$inner_row['id'].');"><td id="new_row_id_'.$inner_row['id'].'"><table width="100%" style=" color:#000000" border="1"><tr><td>New</td><td>'.$inner_row['personal_reference'].'</td><td>'.$inner_row['heading'].'</td></tr></table></td></tr>';
		$i++;
	}
	$finalResult = $startTable.$rowStr.$endTable;
?>
	<script type="text/javascript">
	
		//parent.window.opener.document.getElementById('tdCreateAd_<?=$_SESSION['form_id']?>').innerHTML = '<?php echo $finalResult;?>' ;
		//alert(parent.window.opener.document.getElementById('tdCreateAd_<?=$_SESSION['form_id']?>').innerHTML);
		//self.close();
		top.document.getElementById('tdCreateAd_<?=$_SESSION['form_id']?>').innerHTML = '<?php echo $finalResult;?>' ;
		//top.document.getElementById('no_of_ads').innerHTML = 'Ads: <?php echo $total_ads;?>' ;
		parent.parent.GB_hide();
	</script>
<?php
 }



// to get the type of ad query
$get_type_id = $_SESSION['type_id'] ; //$_POST['typeid'];
$res_type_query = mysql_query("select adspaceType from adspace_details where adspaceId = '".$get_type_id."' ");
$row_type_query = mysql_fetch_array($res_type_query);

if(isset($_POST['post_checking'])){
$adtype_retrieve=$_POST['network_type'];
}
else
$adtype_retrieve=$row_type_query['adspaceType'];
// End to get the type of ad query

// For file upload coding

if ($_FILES["file"]["error"] > 0)
  {
 // echo "Error: " . $_FILES["file"]["error"] . "<br />";
  }
else
  {
  $file_name = $_FILES["file"]["name"];
  $extension = explode('.',$file_name);  
  move_uploaded_file($_FILES["file"]["tmp_name"],"upload/" . $_SESSION[siteuserId] .".".$extension[1]);
  }


// End of file upload query


?>
<script>




// function submit_browse_form()
// {
// frmAvertise.submit();
// }

function main_page()
{
//self.close();
parent.parent.GB_hide();
}

function showhidSelectBox(mode)
{
	if(mode!='none')
	{
		document.getElementById('txtUrl').value=document.getElementById('selExistingAd').value;
	}
	else
	{
		document.getElementById('txtUrl').value='';
		document.getElementById('txtUrl').readOnly=false;
	}
		document.getElementById('tdExistingAd').style.display=mode;
}

function validation()
{
	var txtHeadline = document.getElementById("txtHeadline").value;
	var txtUrl = document.getElementById("txtUrl").value;
	var text_description = document.getElementById("text_description").value; 
	if(text_description != "")    {
        document.getElementById("validate_text_description").style.display="none";
    }

	if(txtHeadline != "")    {
        document.getElementById("txtHeadline_validate").style.display="none";
    }

	if(txtUrl != "")    {
        document.getElementById("txtUrl_validate").style.display="none";
    }

	if(txtHeadline == "")    {
        document.getElementById("txtHeadline_validate").style.display="block";
        //document.getElementById("txtHeadline_validate").innerHTML="Please Enter Headline!";
        document.getElementById("txtHeadline").focus();
        return false;
    }

	if(text_description == "")    {
        document.getElementById("validate_text_description").style.display="block";
        document.getElementById("validate_text_description").innerHTML="Please Enter Desc!";
        document.getElementById("text_description").focus();
        return false;
    }

	if(txtUrl == "")    {
        document.getElementById("txtUrl_validate").style.display="block";
        //document.getElementById("txtUrl_validate").innerHTML="Please Enter Destination Url!";
        document.getElementById("txtUrl").focus();
        return false;
    }
	
	return true;
}

function preview_heading()
{
	document.getElementById("prev_heading").value = document.getElementById("txtHeadline").value;
	document.getElementById("prev_heading").innerHTML= document.getElementById("txtHeadline").value;
	document.getElementById("prev_heading").style.display = 'block';
}

function preview_text_description_func()
{
	document.getElementById("preview_text_description").value = document.getElementById("text_description").value;
	document.getElementById("preview_text_description").innerHTML= document.getElementById("text_description").value;
	document.getElementById("preview_text_description").style.display = 'block';
}

</script>
<br>
<input type="hidden" name="hidAdSpaceId" id="hidAdSpaceId" value="<?php echo $adspaceID;?>">
<input type="hidden" name="hidDelAdSpaceId" id="hidDelAdSpaceId" value="<?php echo $fetFavId[0];?>">
<table width="90%" border="0" cellspacing="0" cellpadding="0" align="center">
              <tr>
                <td valign="top" align="center"><table width="98%" border="0" align="center" cellpadding="0" cellspacing="0">
                  <tr>
                    <td valign="top"></td>
                    <td valign="top" align="center"><table width="100%" border="0" cellspacing="0" cellpadding="0" align="center">
                      <tr>
                        <td valign="top"><table width="100%" border="0" cellspacing="0" cellpadding="0">
                            <tr>
                              <td width="11"><img src="images/PinkBorderTopLeft.jpg" alt="" width="11" height="11"></td>
                              <td class="PinkBoxTopBg"><img src="images/spacer.gif" alt="" width="10" height="11"></td>
                              <td width="11"><img src="images/PinkBorderTopRight.jpg" alt="" width="11" height="11"></td>
                            </tr>
                        </table></td>
                      </tr>
                      <tr>
                        <td class="PinkTwosideBorder" valign="top"><table cellspacing="0" cellpadding="0" border="0">
        <tr>
          <TD><!--Left TD-->
            <table cellspacing="0" cellpadding="0" border="0">
              <TR>
                <TD align="left">&nbsp;</TD>
              </TR>
              <TR>
                <TD align="center"><table style="border:#CCCCCC 1px solid;" cellspacing="0" cellpadding="0" width="95%" align="center">
                    <tr>
                      <td><!--LEFT TD-->
                        <table cellspacing="0" cellpadding="0" border="0">
                          <form name="frmAvertise" id="frmAvertise" method="POST" action="" enctype="multipart/form-data">
                          
                          <input type="hidden" name="hidden_field" id="hidden_field" value="www">
                          <input type="hidden" name="enddate" value="<?php echo $_REQUEST[duration];?>">
                          <input type="hidden" name="adspaceid" value="<?php echo $_REQUEST[id];?>">
                          <tr>
                            <td valign="top" align="left"><table cellpadding="5" cellspacing="0" border="0">
                                <tr>
                                  <td width="300" class="sitename" colspan="2">Create Your Ad</td>
                                </tr>
                                <TR>
                                  <TD align="right"><input checked="true" onclick="showhidSelectBox('none')" type="radio" name="radSelectAd" id="radSelectAd1"></TD>
                                  <td align="left"><strong>Use An existing page</strong></td>
                                </tr>
                                <TR>
                                  <TD align="right"><input onclick="showhidSelectBox('')"  type="radio" name="radSelectAd" id="radSelectAd2"></TD>
                                  <td align="left"><strong>Use a landing page</strong></td>
                                </tr>
                                <?php
			 $query_landing_page="select * from adspace_landing_page where UserId='".$_SESSION['siteuserId']."' ";
			$sql_landing_page=mysql_query($query_landing_page);
			$num_landing_page=mysql_num_rows($sql_landing_page);
			if($num_landing_page>0)
			{
 			?>
                                <tr>
                                  <TD colspan="2" id="tdExistingAd" style="display:none;">&nbsp;
                                    <select name="selExistingAd" id="selExistingAd">
                                      <?php
						  while($res_landing_page= mysql_fetch_array($sql_landing_page)){
                                    ?>
                                      <option value="<?=$GLOBALS['site_url'].$res_landing_page['pageName'].".php";?>">
                                      <?=stripslashes($res_landing_page['pageName']);?>
                                      </option>
                                      <?
                                        }
                                        ?>
                                    </select>
                                    <!--left td-->
                                  </TD>
                                </tr>
                                <?php
			}
			else
			{
			?>
                                <TD colspan="2" id="tdExistingAd" style="display:none;">&nbsp;
                                    You do not have any Landing Pages. <a href="create_landing_page.php" class="register_link">Create a new Landing Page</a> </td>
                                  <?php
			}
			?>
                                <tr>
                                  <TD colspan="2">&nbsp;*Headline:</TD>
                                </tr>
                                <tr>
                                  <TD colspan="2">&nbsp;
                                    <input type="text" name="txtHeadline" id="txtHeadline" onkeyup="return preview_heading();"></TD>
                                  <TD colspan="2" id="txtHeadline_validate" style="display:none;">Please Enter Url!</TD>
                                </tr>
                                <tr>
                                  <TD colspan="2">&nbsp;*Add Description</TD>
                                </tr>
                                <tr>
                                  <TD colspan="2">&nbsp;
                                    <textareA name="text_description" id="text_description" onkeyup="return preview_text_description_func();"></textareA></TD>
                                  <TD colspan="2" id="validate_text_description" style="display:none;">Please Enter Description !</TD>
                                </tr>
                                <? if($adtype_retrieve == 'banner'){ ?>
                                <tr>
                                  <TD colspan="2">&nbsp;*Upload File:  (Max. 90 KB) (728 x 90 preferred)</TD>
                                </tr>
                                <tr>
                                  <TD colspan="2"><input type="file" name="file" id="file" onclick="return submit_browse_form();" />
                                    <inpUT type="hidden" name="sahara" value="11">
                                  </TD>
                                </tr>
                                <? } ?>
                                <tr>
                                  <TD colspan="2">&nbsp;*Destination URL (http://):</TD>
                                </tr>
                                <tr>
                                  <TD colspan="2">&nbsp;
                                    <input type="text" name="txtUrl" id="txtUrl"></TD>
                                  <TD colspan="2" id="txtUrl_validate" style="display:none;">Please Enter Destiny url!</TD>
                                </tr>
                                <tr>
                                  <TD colspan="2">&nbsp;Ad Name (For personal reference)</TD>
                                </tr>
                                <tr>
                                  <TD colspan="2">&nbsp;
                                    <input type="text" name="txtAdName" id="txtAdName"></TD>
                                </tr>
                                <tr>
                                  <TD colspan="2">&nbsp;</TD>
                                </tr>
                                <tr>
                                  <TD colspan="2">&nbsp;
                                    <input type="button" value="Cancel" onclick="return main_page();">
                                    &nbsp;
                                    <input type="submit" value="Save" name="addtocart" onclick="return validation('<?php echo $adtype_retrieve;?>');"></TD>
                                </tr>
                                <tr>
                                  <TD colspan="2">&nbsp;</TD>
                                </tr>
                              </table></td>
                            <td width="50px" valign="top"></td>
                            <!--Middle TD-->
                            <td valign="top"><!--Right TD START-->
                              <table>
                                <tr valign="top">
                                  <td class="sitename" valign="top" align="center">Preview</td>
                                </tr>
                                <tr>
                                  <td align="center" id="prev_heading"></td>
                                </tr>
                                <tr>
                                  <td align="center" id="preview_text_description"></td>
                                </tr>
                                <tr>
                                  <td>Please note: The Preview will not display correctly if you do not set the "Text size" to "Normal" on your browser.</td>
                                </tr>
                              </table></td>
                            <!--Right TD END-->
                          </tr>
						  
						  </form>
                        </table></td>
                    </tr>
                    
                    
                  </table></TD>
            </table></TD>
        </tr>
      </table></td>
                      </tr>
                      <tr>
                        <td><table width="100%" border="0" cellspacing="0" cellpadding="0">
                            <tr>
                              <td width="11"><img src="images/PinkBorderBottomLeft.jpg" alt="" width="11" height="11"></td>
                              <td class="PinkBoxBottomBg"><img src="images/spacer.gif" alt="" width="10" height="11"></td>
                              <td width="11"><img src="images/PinkBorderBottomRight.jpg" alt="" width="11" height="11"></td>
                            </tr>
                        </table></td>
                      </tr>
                    </table></td>
                    <td>&nbsp;</td>
                    </tr>
                </table></td>
              </tr>
            </table>
<script>
function ad_ads(txtHeadline,text_description,txtUrl,txtAdName)
{

var xmlHttp;
try
  {
  // Firefox, Opera 8.0+, Safari
  xmlHttp=new XMLHttpRequest();
  }
catch (e)
  {
  // Internet Explorer
  try
    {
    xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
    }
  catch (e)
    {
    try
      {
      xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
      }
    catch (e)
      {
      alert("Your browser does not support AJAX!");
      return false;
      }
    }
  }
//   xmlHttp.onreadystatechange=function()
//     {
//     if(xmlHttp.readyState==4)
//       {
// alert(xmlHttp.responseText);
// 	  
//       }
//     }



  xmlHttp.open("GET","add_adgroup_ad.php?txtHeadline="+txtHeadline+"&text_description="+text_description+"&txtUrl="+txtUrl+"&txtAdName="+txtAdName,true);
  xmlHttp.send(null);

  //return false; 
}


</script>
